home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / s342q12.lha / minrexx.c < prev    next >
C/C++ Source or Header  |  1995-06-04  |  15KB  |  433 lines

  1. /*
  2.  *   This is an example of how REXX messages might be handled.  This is
  3.  *   a `minimum' example that both accepts asynchronous REXX messages and
  4.  *   can request REXX service.
  5.  *
  6.  *   Read this entire file!  It's short enough.
  7.  *
  8.  *   It is written in such a fashion that it can be attached to a program
  9.  *   with a minimum of fuss.  The only external symbols it makes available
  10.  *   are the seven functions and RexxSysBase.
  11.  *
  12.  *   This code is by Radical Eye Software, but it is put in the public
  13.  *   domain.  I would appreciate it if the following string was left in
  14.  *   both as a version check and as thanks from you for the use of this
  15.  *   code.
  16.  *
  17.  *   If you modify this file for your own use, don't bump the version
  18.  *   number; add a suffix, such as 1.0a or 1.0.3 or something, so we
  19.  *   don't have fake `versions' floating around.
  20.  */
  21. static char *blurb = "Radical Eye MinRexx 0.4" ;
  22. /*
  23.  *   We read in our own personal little include.
  24.  */
  25. #include "minrexx.h"
  26. /*
  27.  *   All of our local globals, hidden from sight.
  28.  */
  29. static struct MsgPort *rexxPort ;          /* this is *our* rexx port */
  30. static int bringerdown ;                   /* are we trying to shut down? */
  31. static struct rexxCommandList *globalrcl ; /* our command association list */
  32. static long stillNeedReplies ;             /* how many replies are pending? */
  33. static long rexxPortBit ;                  /* what bit to wait on for Rexx? */
  34. static char *extension ;                   /* the extension for macros */
  35. static void (*userdisp)(struct RexxMsg *, struct rexxCommandList *, char *) ;
  36.                                            /* the user's dispatch function */
  37. static struct RexxMsg *oRexxMsg ;          /* the outstanding Rexx message */
  38. /*
  39.  *   This is the main entry point into this code.
  40.  */
  41. long upRexxPort(
  42. /*
  43.  *   The first argument is the name of your port to be registered;
  44.  *   this will be used, for instance, with the `address' command of ARexx.
  45.  */
  46. char *s,
  47. /*
  48.  *   The second argument is an association list of command-name/user-data
  49.  *   pairs.  It's an array of struct rexxCommandList, terminated by a
  50.  *   structure with a NULL in the name field. The commands are case
  51.  *   sensitive.  The user-data field can contain anything appropriate,
  52.  *   perhaps a function to call or some other data.
  53.  */
  54. struct rexxCommandList *rcl,
  55. /*
  56.  *   The third argument is the file extension for ARexx macros invoked
  57.  *   by this program.  If you supply this argument, any `primitive' not
  58.  *   in the association list rcl will be sent out to ARexx for
  59.  *   interpretation, thus allowing macro programs to work just like
  60.  *   primitives.  If you do not want this behavior, supply a `NULL'
  61.  *   here, and those commands not understood will be replied with an
  62.  *   error value of RXERRORNOCMD.
  63.  */
  64. char *exten,
  65. /*
  66.  *   The fourth argument is the user dispatch function.  This function
  67.  *   will *only* be called from rexxDisp(), either from the user calling
  68.  *   this function directly, or from dnRexxPort().  Anytime a command
  69.  *   match is found in the association list, this user-supplied function
  70.  *   will be called with two arguments---the Rexx message that was
  71.  *   received, and a pointer to the association pair.  This function
  72.  *   should return a `1' if the message was replied to by the function
  73.  *   and a `0' if the default success code of (0, 0) should be returned.
  74.  *   Note that the user function should never ReplyMsg() the message;
  75.  *   instead he should indicate the return values with replyRexxCmd();
  76.  *   otherwise we lose track of the messages that still lack replies.
  77.  */
  78. void (*uf)(struct RexxMsg *, struct rexxCommandList *, char *))
  79. /*
  80.  *   upRexxPort() returns the signal bit to wait on for Rexx messages.
  81.  *   If something goes wrong, it simply returns a `0'.  Note that this
  82.  *   function is safe to call multiple times because we check to make
  83.  *   sure we haven't opened already.  It's also a quick way to change
  84.  *   the association list or dispatch function.
  85.  */
  86. {
  87. /*
  88.  *   Some basic error checking.
  89.  */
  90.    if (rcl == NULL || uf == NULL)
  91.       return(0L) ;
  92. /*
  93.  *   If we aren't open, we make sure no one else has opened a port with
  94.  *   this name already.  If that works, and the createport succeeds, we
  95.  *   fill rexxPortBit with the value to return.
  96.  *
  97.  *   Note that rexxPortBit will be 0 iff rexxPort is NULL, so the check
  98.  *   for rexxPort == NULL also insures that our rexxPortBit is 0.
  99.  */
  100.    if (rexxPort == NULL) {
  101.       Forbid() ;
  102.       if (FindPort(s)==NULL)
  103.          rexxPort = CreatePort(s, 0L) ;
  104.       Permit() ;
  105.       if (rexxPort != NULL)
  106.          rexxPortBit = 1L << rexxPort->mp_SigBit ;
  107.    }
  108. /*
  109.  *   Squirrel away these values for our own internal access, and return
  110.  *   the wait bit.
  111.  */
  112.    globalrcl = rcl ;
  113.    extension = exten ;
  114.    userdisp = uf ;
  115.    return(rexxPortBit) ;
  116. }
  117. /*
  118.  *   This function closes the rexx library, but only if it is open
  119.  *   and we aren't expecting further replies from REXX.  It's
  120.  *   *private*, but it doesn't have to be; it's pretty safe to
  121.  *   call anytime.
  122.  */
  123. static void closeRexxLib(void) {
  124.    if (stillNeedReplies == 0 && RexxSysBase) {
  125.       CloseLibrary((struct Library *)RexxSysBase) ;
  126.       RexxSysBase = NULL ;
  127.    }
  128. }
  129. /*
  130.  *   This function closes down the Rexx port.  It is always safe to
  131.  *   call, and should *definitely* be made a part of your cleanup
  132.  *   routine.  No arguments and no return.  It removes the Rexx port,
  133.  *   replies to all of the messages and insures that we get replies
  134.  *   to all the ones we sent out, closes the Rexx library, deletes the
  135.  *   port, clears a few flags, and leaves.
  136.  */
  137. void dnRexxPort(void) {
  138.    if (rexxPort) {
  139.       RemPort(rexxPort) ;
  140.       bringerdown = 1 ;
  141. /*
  142.  *   A message still hanging around?  We kill it off.
  143.  */
  144.       if (oRexxMsg) {
  145.          oRexxMsg->rm_Result1 = RXERRORIMGONE ;
  146.          ReplyMsg((struct Message *)oRexxMsg) ;
  147.          oRexxMsg = NULL ;
  148.       }
  149.       while (stillNeedReplies) {
  150.          WaitPort(rexxPort) ;
  151.          dispRexxPort() ;
  152.       }
  153.       closeRexxLib() ;
  154.       DeletePort(rexxPort) ;
  155.       rexxPort = NULL ;
  156.    }
  157.    rexxPortBit = 0 ;
  158. }
  159. /*
  160.  *   Here we dispatch any REXX messages that might be outstanding.
  161.  *   This is the main routine for handling Rexx messages.
  162.  *   This function is fast if no messages are outstanding, so it's
  163.  *   pretty safe to call fairly often.
  164.  *
  165.  *   If we are bring the system down and flushing messages, we reply
  166.  *   with a pretty serious return code RXERRORIMGONE.
  167.  *
  168.  *   No arguments, no returns.
  169.  */
  170. static int cmdcmp(char *, char *) ;
  171. static int dontreply ;
  172. void dispRexxPort(void) {
  173.    register struct RexxMsg *RexxMsg ;
  174.    register struct rexxCommandList *rcl ;
  175.    register char *p ;
  176.  
  177. /*
  178.  *   If there's no rexx port, we're out of here.
  179.  */
  180.    if (rexxPort == NULL)
  181.       return ;
  182. /*
  183.  *   Otherwise we have our normal loop on messages.
  184.  */
  185.    while (RexxMsg = (struct RexxMsg *)GetMsg(rexxPort)) {
  186. /*
  187.  *   If we have a reply to a message we sent, we look at the second
  188.  *   argument.  If it's set, it's a function we are supposed to call
  189.  *   so we call it.  Then, we kill the argstring and the message
  190.  *   itself, decrement the outstanding count, and attempt to close
  191.  *   down the Rexx library.  Note that this call only succeeds if
  192.  *   there are no outstanding messages.  Also, it's pretty quick, so
  193.  *   don't talk to me about efficiency.
  194.  */
  195.       if (RexxMsg->rm_Node.mn_Node.ln_Type == NT_REPLYMSG) {
  196.          if (RexxMsg->rm_Args[1])
  197.             ((int (*)(struct RexxMsg *))(RexxMsg->rm_Args[1]))(RexxMsg) ;
  198.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  199.          DeleteRexxMsg(RexxMsg) ;
  200.          stillNeedReplies-- ;
  201.          closeRexxLib() ;
  202. /*
  203.  *   The default case is we got a message and we need to check it for
  204.  *   primitives.  We skip past any initial tabs or spaces and initialize
  205.  *   the return code fields.
  206.  */
  207.       } else {
  208.          p = (char *)RexxMsg->rm_Args[0] ;
  209.          while (*p > 0 && *p <= ' ')
  210.             p++ ;
  211.          RexxMsg->rm_Result1 = 0 ;
  212.          RexxMsg->rm_Result2 = 0 ;
  213. /*
  214.  *   If somehow the reply is already done or postponed, `dontreply' is
  215.  *   set.
  216.  */
  217.          dontreply = 0 ;
  218. /*
  219.  *   If the sky is falling, we just blow up and replymsg.
  220.  */
  221.          if (bringerdown) {
  222.             RexxMsg->rm_Result1 = RXERRORIMGONE ;
  223. /*
  224.  *   Otherwise we cdr down our association list, comparing commands,
  225.  *   until we get a match.  If we get a match, we call the dispatch
  226.  *   function with the appropriate arguments, and break out.
  227.  */
  228.          } else {
  229.             oRexxMsg = RexxMsg ;
  230.             for (rcl = globalrcl; rcl->name; rcl++) {
  231.                if (cmdcmp(rcl->name, p) == 0) {
  232.                   userdisp(RexxMsg, rcl, p+strlen(rcl->name)) ;
  233.                   break ;
  234.                }
  235.             }
  236. /*
  237.  *   If we broke out, rcl will point to the command we executed; if we
  238.  *   are at the end of the list, we didn't understand the command.  In
  239.  *   this case, if we were supplied an extension in upRexxPort, we know
  240.  *   that we should send the command out, so we do so, synchronously.
  241.  *   The synchronous send takes care of our reply.  If we were given a
  242.  *   NULL extension, we bitch that the command didn't make sense to us.
  243.  */
  244.             if (rcl->name == NULL) {
  245.                if (extension) {
  246.                   syncRexxCmd(RexxMsg->rm_Args[0], RexxMsg) ;
  247.                   dontreply = 1 ;
  248.                } else {
  249.                   RexxMsg->rm_Result1 = RXERRORNOCMD ;
  250.                }
  251.             }
  252.          }
  253. /*
  254.  *   Finally, reply if appropriate.
  255.  */
  256.          oRexxMsg = NULL ;
  257.          if (! dontreply)
  258.             ReplyMsg((struct Message *)RexxMsg) ;
  259.       }
  260.    }
  261. }
  262. /*
  263.  *   This is the function we use to see if the command matches
  264.  *   the command string.  Not case sensitive, and the real command only
  265.  *   need be a prefix of the command string.  Make sure all commands
  266.  *   are given in lower case!
  267.  */
  268. static int cmdcmp(char *c, char *m) {
  269.    while (*c && ((*c == *m) || (*c == *m + 32 && ('a' <= *c && *c <= 'z')))) {
  270.       c++ ;
  271.       m++ ;
  272.    }
  273.    return((int)*c) ;
  274. }
  275. /*
  276.  *   Opens the Rexx library if unopened.  Returns success (1) or
  277.  *   failure (0).  This is another function that is *private* but
  278.  *   that doesn't have to be.
  279.  */
  280. static int openRexxLib(void) {
  281.    if (RexxSysBase)
  282.       return(1) ;
  283.    return((RexxSysBase = (struct Library *)OpenLibrary(RXSNAME, 0L)) != NULL) ;
  284. }
  285. /*
  286.  *   This is the general ARexx command interface, but is not the one
  287.  *   you will use most of the time; ones defined later are easier to
  288.  *   understand and use.  But they all go through here.
  289.  */
  290. struct RexxMsg *sendRexxCmd(
  291. char *s,
  292. /*
  293.  *   The first parameter is the command to send to Rexx.
  294.  */
  295. void (*f)(struct RexxMsg *),
  296. /*
  297.  *   The second parameter is either NULL, indicating that the command
  298.  *   should execute asynchronously, or a function to be called when the
  299.  *   message we build up and send out here finally returns.  Please note
  300.  *   that the function supplied here could be called during cleanup after
  301.  *   a fatal error, so make sure it is `safe'.  This function always is
  302.  *   passed one argument, the RexxMsg that is being replied.
  303.  */
  304. STRPTR p1, STRPTR p2, STRPTR p3)
  305. /*
  306.  *   These are up to three arguments to be stuffed into the RexxMsg we
  307.  *   are building up, making the values available when the message is
  308.  *   finally replied to.  The values are stuffed into Args[2]..Args[4].
  309.  */
  310. {
  311.    struct RexxMsg *CreateRexxMsg() ;
  312.    STRPTR CreateArgstring() ;
  313.    register struct MsgPort *rexxport ;
  314.    register struct RexxMsg *RexxMsg ;
  315.  
  316. /*
  317.  *   If we have too many replies out there, we just return failure.
  318.  *   Note that you should check the return code to make sure your
  319.  *   message got out!  Then, we forbid, and make sure that:
  320.  *      - we have a rexx port open
  321.  *      - Rexx is out there
  322.  *      - the library is open
  323.  *      - we can create a message
  324.  *      - we can create an argstring
  325.  *
  326.  *   If all of these succeed, we stuff a few values and send the
  327.  *   message, permit, and return.
  328.  */
  329.    if (rexxPort == NULL || stillNeedReplies > MAXRXOUTSTANDING-1)
  330.       return(NULL) ;
  331.    RexxMsg = NULL ;
  332.    if (openRexxLib() && (RexxMsg =
  333.              CreateRexxMsg(rexxPort, extension, rexxPort->mp_Node.ln_Name)) &&
  334.              (RexxMsg->rm_Args[0] = CreateArgstring(s, (long)strlen(s)))) {
  335.       RexxMsg->rm_Action = RXCOMM ;
  336.       RexxMsg->rm_Args[1] = (STRPTR)f ;
  337.       RexxMsg->rm_Args[2] = p1 ;
  338.       RexxMsg->rm_Args[3] = p2 ;
  339.       RexxMsg->rm_Args[4] = p3 ;
  340.       RexxMsg->rm_Node.mn_Node.ln_Name = RXSDIR ;
  341.       Forbid() ;
  342.       if (rexxport = FindPort(RXSDIR))
  343.          PutMsg(rexxport, (struct Message *)RexxMsg) ;
  344.       Permit() ;
  345.       if (rexxport) {
  346.          stillNeedReplies++ ;
  347.          return(RexxMsg) ;
  348.       } else
  349.          DeleteArgstring(RexxMsg->rm_Args[0]) ;
  350.    }
  351.    if (RexxMsg)
  352.       DeleteRexxMsg(RexxMsg) ;
  353.    closeRexxLib() ;
  354.    return(NULL) ;
  355. }
  356. /*
  357.  *   This function is used to send out an ARexx message and return
  358.  *   immediately.  Its single parameter is the command to send.
  359.  */
  360. struct RexxMsg *asyncRexxCmd(char *s) {
  361.    return(sendRexxCmd(s, NULL, NULL, NULL, NULL)) ;
  362. }
  363. /*
  364.  *   This function sets things up to reply to the message that caused
  365.  *   it when we get a reply to the message we are sending out here.
  366.  *   But first the function we pass in, which actually handles the reply.
  367.  *   Note how we get the message from the Args[2]; Args[0] is the command,
  368.  *   Args[1] is this function, and Args[2]..Args[4] are any parameters
  369.  *   passed to sendRexxCmd() as p1..p3.  We pass the result codes right
  370.  *   along.
  371.  */
  372. static void replytoit(struct RexxMsg *msg) {
  373.    register struct RexxMsg *omsg ;
  374.  
  375.    omsg = (struct RexxMsg *)(msg->rm_Args[2]) ;
  376.    replyRexxCmd(omsg, msg->rm_Result1, msg->rm_Result2, NULL) ;
  377.    ReplyMsg((struct Message *)omsg) ;
  378. }
  379. /*
  380.  *   This function makes use of everything we've put together so far,
  381.  *   and functions as a synchronous Rexx call; as soon as the macro
  382.  *   invoked here returns, we reply to `msg', passing the return codes
  383.  *   back.
  384.  */
  385. struct RexxMsg *syncRexxCmd(char *s, struct RexxMsg *msg) {
  386.    return(sendRexxCmd(s, replytoit, (STRPTR)msg, NULL, NULL)) ;
  387. }
  388. /*
  389.  *   There are times when you want to pass back return codes or a
  390.  *   return string; call this function when you want to do that,
  391.  *   and return `1' from the user dispatch function so the main
  392.  *   event loop doesn't reply (because we reply here.)  This function
  393.  *   always returns 1.
  394.  */
  395. void replyRexxCmd(
  396. /*
  397.  *   The first parameter is the message we are replying to.
  398.  */
  399. register struct RexxMsg *msg,
  400. /*
  401.  *   The next two parameters are the primary and secondary return
  402.  *   codes.
  403.  */
  404. register long primary, long secondary,
  405. /*
  406.  *   The final parameter is a return string.  This string is only
  407.  *   returned if the primary return code is 0, and a string was
  408.  *   requested.
  409.  *
  410.  *   We also note that we have replied to the message that came in.
  411.  */
  412. register char *string) {
  413. /*
  414.  *   Note how we make sure the Rexx Library is open before calling
  415.  *   CreateArgstring . . . and we close it down at the end, if possible.
  416.  */
  417.    if (primary == 0 && (msg->rm_Action & (1L << RXFB_RESULT))) {
  418.       if (string && openRexxLib())
  419.          secondary = (long)CreateArgstring(string, (long)strlen(string)) ;
  420.       else
  421.          secondary = 0L ;
  422.    }
  423.    msg->rm_Result1 = primary ;
  424.    msg->rm_Result2 = secondary ;
  425.    closeRexxLib() ;
  426. }
  427. /*
  428.  *   If we don't want to reply, we call this.
  429.  */
  430. void DontReply(void) {
  431.    dontreply = 1 ;
  432. }
  433.